import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'action.dart';
import 'state.dart';

var controller = TextEditingController();

Widget buildView(LoginPhoneState state, Dispatch dispatch,
    ViewService viewService) {
  bool isSelect = state.phone.length == 11;
  return GestureDetector(
    onTap: () {
      FocusScope.of(viewService.context)
          .requestFocus(FocusNode()); //点击空白处键盘消失
    },
    child: Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        brightness: Brightness.light,
        backgroundColor: Colors.white, //导航栏和状态栏的的颜色
        elevation: 0, //阴影的高度
        leading: BackButton(
          color: Color.fromRGBO(160, 160, 160, 1),
          onPressed: () {
            Navigator.pop(viewService.context);
          },
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,

        children: [
          Padding(
              padding: EdgeInsets.only(top: 36, left: 30),
              child: Text(
                '请输入手机号',
                style: TextStyle(
                    color: Color.fromRGBO(47, 47, 47, 1), fontSize: 30),
              )),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 30),
            margin: EdgeInsets.only(top: 67),
            child: Row(
              children: [
                Text(
                  '+86',
                  style: TextStyle(
                      color: Color.fromRGBO(153, 153, 153, 1), fontSize: 18),
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                    child: TextField(
                        controller: controller,
                        keyboardType: TextInputType.number,
                        // maxLines: 1,
                        //最大行数
                        // autocorrect: true,//是否自动更正
                        // autofocus: true,//是否自动对焦
                        // obscureText: true,//是否是密码
                        textAlign: TextAlign.left,
                        //文本对齐方式
                        style: TextStyle(
                            fontSize: 20,
                            color: Color.fromRGBO(47, 47, 47, 1)),
                        //输入文本的样式
                        onChanged: (text) {
                          //内容改变的回调
                          print('change $text');
                          // bool isPhone;
                          // if(text.length == 11){
                          //   isPhone = true;
                          // }else{
                          //   isPhone = false;
                          // }
                          dispatch(LoginPhoneActionCreator.upPhone(text));
                        },
                        decoration: InputDecoration.collapsed(
                            hintText: '请输入手机号码',
                            hintStyle: TextStyle(
                                color: Color.fromRGBO(204, 204, 204, 1),
                                fontSize: 17)))),
              ],
            ),
          ),
          Container(
            color: Color.fromRGBO(204, 204, 204, 1),
            height: 0.5,
            margin: EdgeInsets.only(top: 15, right: 30, left: 30),
          ),
          Container(
            padding: EdgeInsets.only(top: 10),
            margin: EdgeInsets.symmetric(horizontal: 30),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                InkWell(
                  onTap: () {
                    bool isP = !state.isPrivacy;
                    dispatch(LoginPhoneActionCreator.upPrivacy(isP));
                  },
                  child: Container(
                    height: 20,
                    width: 24,
                    alignment: Alignment.center,
                    child: Image.asset(
                      state.isPrivacy
                          ? "assets/images/auth_module/login_check.png"
                          : "assets/images/auth_module/login_check_un.png",
                      width: 14,
                      height: 14,
                    ),),
                ),
                Expanded(
                    child: RichText(
                      text: TextSpan(style: TextStyle(fontSize: 12), children: [
                        TextSpan(
                            text: "我已详细阅读并同意",
                            style: TextStyle(color: Color(0xFF999999))),
                        TextSpan(
                          text: "《券后价用户协议》",
                          style: TextStyle(color: Color(0xFFFD294A)),
                          recognizer: TapGestureRecognizer()
                            ..onTap = () {
                              print("跳转到用户协议...");
                            },
                        ),
                        TextSpan(
                            text: "与",
                            style: TextStyle(color: Color(0xFF999999))),
                        TextSpan(
                          text: "《券后价隐私协议》",
                          style: TextStyle(color: Color(0xFFFD294A)),
                          recognizer: TapGestureRecognizer()
                            ..onTap = () {
                              print("跳转到隐私协议...");
                            },
                        ),
                      ]),
                    ))
              ],
            ),
          ),
          SizedBox(
            height: 20,
          ),
          Container(
            alignment: Alignment.center,
            child: GestureDetector(
              onTap: () {
               // if (state.isPrivacy && isSelect) {
                  Navigator.pushNamed(
                    viewService.context, 'login_verify', arguments: {
                    "phone": state.phone,
                  },
                  );
                // } else {
                //   if(!isSelect&&!state.isPrivacy){
                //     _toast("请填写手机号和查看协议");
                //   }
                //
                //   if (!isSelect && state.isPrivacy ) {
                //       _toast("请输入正确手机号");
                //   }
                //   if (!state.isPrivacy&&isSelect) {
                //       _toast("请查看协议");
                //   }
                // }
              },
              child: Image.asset(
                isSelect && state.isPrivacy
                    ? 'assets/images/auth_module/login_button.png'
                    :
                'assets/images/auth_module/login_button_un.png',
                width: 85,
                height: 85,
              ),
            ),
          ),
        ],
      ),
    ),);
}
_toast( String msg){
  Fluttertoast.showToast(
    msg: msg,
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIos: 1,
    backgroundColor: Colors.black,
    textColor: Colors.white,
    fontSize: 16.0,
  );
}